/* 
Common include for the LTE Game Engine examples, here there are the functions
to setup psp.
*/

#define PSP_BOOST_CPU
// Undefine this if you don't want to
// overclock your cpu to 333MHz

#include <pspkernel.h>
#ifdef PSP_BOOST_CPU
#include <psppower.h>
#endif

#ifdef PSP_ENABLE_DEBUG
#include <pspdebug.h>
#define printf pspDebugScreenPrintf
#endif


/* Declare a module.  This must be specified in the source of a library or executable. */
//PSP_MODULE_INFO(name, attributes, major_version, minor_version)
//10 means probably starts in kernel mode
//see http://forums.ps2dev.org/viewtopic.php?t=8917
PSP_MODULE_INFO("PSPCLIENT", 10, 1, 1);


PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU);
PSP_MAIN_THREAD_PRIORITY(0x10);

//PSP_HEAP_SIZE_MAX();

PSP_HEAP_SIZE_KB(20000);

int exitCallback(int arg1, int arg2, void *common)
{
    sceKernelExitGame();
    return 0;
}

int callbackThread(SceSize args, void *argp)
{
    int cbid;

    cbid = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
    sceKernelRegisterExitCallback(cbid);

    sceKernelSleepThreadCB();

    return 0;
}

int setupCallbacks(void)
{
    int thid = 0;

    thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
    
    if(thid >= 0)
    {
        sceKernelStartThread(thid, 0, 0);
    }

    return thid;
}

int setupPSP(void)
{
#ifdef PSP_BOOST_CPU
    scePowerSetClockFrequency(333, 333, 166);
#endif

    // setup callbacks
    setupCallbacks();

#ifdef PSP_ENABLE_DEBUG
    pspDebugScreenInit();
#endif

}
